/*---------------------------------------------------------------------------------

	by GugloPWN

---------------------------------------------------------------------------------*/

#include "sub_screen.h"

#include "Bricks_Tile01.h"
#include "bigbitmap.h"
#include "Bricks_Alpha.h"

//Defines courtesy of LiraNuna
	// Set of defines for tile flipping
#define TILE_FLIP_NONE    (0 << 10)
#define TILE_FLIP_X       (1 << 10)
#define TILE_FLIP_Y       (2 << 10)
#define TILE_FLIP_XY      (TILE_FLIP_X | TILE_FLIP_Y)
 
	// Macro for palette selecting
#define TILE_PAL(n) ((n) << 12)




void subscrn::init_subscreen() //Some code thanks to Dovoto
{

	int i,j;
 
	//set video mode and map vram to the background
	//videoSetModeSub(MODE_5_2D | DISPLAY_SPR_ACTIVE | DISPLAY_SPR_1D | DISPLAY_BG3_ACTIVE); 
	videoSetModeSub(MODE_3_2D);
	vramSetBankC(VRAM_C_SUB_BG_0x06200000);
	vramSetBankI(VRAM_I_SUB_SPRITE);
	

 
	bg0 = bgInitSub(0, BgType_Text4bpp, BgSize_T_256x256, 29, 3);
 
	u16* tileMemory = bgGetGfxPtr(bg0);
    u16* mapMemory = bgGetMapPtr(bg0);
	
	int bg1 = bgInitSub(1, BgType_Text4bpp, BgSize_T_256x256, 30, 2);
 
    u16* mapMemory_alpha = bgGetMapPtr(bg1);
 
	//create a map in map memory
	for(i = 0; i < 32; i++)
	{
		for(j = 0; j < 32; j++)
		{
			//mapMemory[i*32+j] = i%8*8 + j%8;
			mapMemory[i*32+j] = (i%8*8 + j%8) | TILE_FLIP_NONE | TILE_PAL(1);
			mapMemory_alpha[i*32+j] = (i%8*8 + j%8) | TILE_FLIP_NONE | TILE_PAL(2);
		}
	}

	dmaCopy(bigbitmapPal, BG_PALETTE_SUB, sizeof(bigbitmapPal));
	//define a palette for black and white bricks
	BG_PALETTE_SUB[32] = RGB15(0, 0, 0);
	BG_PALETTE_SUB[33] = RGB15(0, 0, 0);
	BG_PALETTE_SUB[34] = RGB15(0, 0, 0);
	BG_PALETTE_SUB[35] = RGB15(0, 0, 0);
	BG_PALETTE_SUB[36] = RGB15(0, 0, 0);
	BG_PALETTE_SUB[37] = RGB15(0, 0, 0);
	BG_PALETTE_SUB[38] = RGB15(0, 0, 0);
	BG_PALETTE_SUB[39] = RGB15(0, 0, 0);
	BG_PALETTE_SUB[40] = RGB15(0, 0, 0);
	BG_PALETTE_SUB[41] = RGB15(0, 0, 0);
	BG_PALETTE_SUB[42] = RGB15(15, 15, 15);
	BG_PALETTE_SUB[43] = RGB15(18, 18, 18);
	BG_PALETTE_SUB[44] = RGB15(22, 22, 22);
	BG_PALETTE_SUB[45] = RGB15(26, 26, 26);
	BG_PALETTE_SUB[46] = RGB15(28, 28, 28);
	BG_PALETTE_SUB[47] = RGB15(31, 31, 31);
	
	
	

	//Load up the tiles and palette with dma system!
	

	dmaCopy(Bricks_Tile01Tiles, tileMemory, sizeof(Bricks_Tile01Tiles));
	dmaCopy(Bricks_AlphaTiles, bgGetGfxPtr(bg1), sizeof(Bricks_AlphaTiles));
	dmaCopy(Bricks_Tile01Pal, BG_PALETTE_SUB+16, sizeof(Bricks_Tile01Pal));
	dmaCopy(Bricks_Tile01Pal, BG_PALETTE_SUB, sizeof(u16));


	bg3 = bgInitSub(3, BgType_Bmp8, BgSize_B8_256x256, 4,0);

	dmaCopy(bigbitmapBitmap, bgGetGfxPtr(bg3), sizeof(bigbitmapBitmap));
	//dmaCopy(bigbitmapPal, BG_PALETTE_SUB, sizeof(bigbitmapPal));


	bmpbuffer = (u8*)malloc(49152);


	pal_subscreen();
	update_subscreen();
	
	//bgHide(bg1);

	bgSetPriority(bg0,3);
	bgSetPriority(bg3,2);
	bgSetPriority(bg1,1);


	REG_BLDCNT_SUB = BLEND_ALPHA | BLEND_SRC_BG1 | BLEND_DST_BG3;
    //REG_BLDCNT_SUB = BLEND_ALPHA | BLEND_SRC_BG1 | BLEND_DST_BG3;
	
    REG_BLDALPHA_SUB = (2) + (16<<8);
	//REG_BLDY_SUB = (12) + (16<<8);


	return;
}

void subscrn::draw_subscreen()
{
	// read the touchscreen coordinates
	touchRead(&touch);
	int tx=0, ty=0;
	if ((keysHeld() & KEY_TOUCH))
	{
		if(touch.px < 10) tx = 10;
		else if(touch.px > 245) tx = 245;
		else tx = touch.px;
		
		if(touch.py < 10) ty = 10;
		else if(touch.py > 181) ty = 181;
		else ty = touch.py;
		
		
		bmpbuffer[tx+256*ty] = (u8)252;
		
		bmpbuffer[tx+1+256*ty] = (u8)252;
		bmpbuffer[tx-1+256*ty] = (u8)252;
		bmpbuffer[tx+256*(ty+1)] = (u8)252;
		bmpbuffer[tx+256*(ty-1)] = (u8)252;
		
		bmpbuffer[tx+1+256*(ty+1)] = (u8)252;
		bmpbuffer[tx+1+256*(ty-1)] = (u8)252;
		bmpbuffer[tx-1+256*(ty+1)] = (u8)252;
		bmpbuffer[tx-1+256*(ty-1)] = (u8)252;
		
		bmpbuffer[tx+2+256*(ty-1)] = (u8)252;
		bmpbuffer[tx+2+256*(ty+1)] = (u8)252;
		bmpbuffer[tx+2+256*(ty)] = (u8)252;
		bmpbuffer[tx+2+256*(ty-2)] = (u8)252;
		bmpbuffer[tx+2+256*(ty+2)] = (u8)252;
		
		bmpbuffer[tx-2+256*(ty-1)] = (u8)252;
		bmpbuffer[tx-2+256*(ty+1)] = (u8)252;
		bmpbuffer[tx-2+256*(ty)] = (u8)252;
		bmpbuffer[tx-2+256*(ty-2)] = (u8)252;
		bmpbuffer[tx-2+256*(ty+2)] = (u8)252;
		
		bmpbuffer[tx+1+256*(ty+2)] = (u8)252;
		bmpbuffer[tx-1+256*(ty+2)] = (u8)252;
		bmpbuffer[tx+256*(ty+2)] = (u8)252;
		
		bmpbuffer[tx+1+256*(ty-2)] = (u8)252;
		bmpbuffer[tx-1+256*(ty-2)] = (u8)252;
		bmpbuffer[tx+256*(ty-2)] = (u8)252;
		
		bmpbuffer[tx+3+256*(ty-1)] = (u8)252;
		bmpbuffer[tx+3+256*(ty+1)] = (u8)252;
		bmpbuffer[tx+3+256*(ty)] = (u8)252;
		bmpbuffer[tx+3+256*(ty-2)] = (u8)252;
		bmpbuffer[tx+3+256*(ty+2)] = (u8)252;
		
		bmpbuffer[tx-3+256*(ty-1)] = (u8)252;
		bmpbuffer[tx-3+256*(ty+1)] = (u8)252;
		bmpbuffer[tx-3+256*(ty)] = (u8)252;
		bmpbuffer[tx-3+256*(ty-2)] = (u8)252;
		bmpbuffer[tx-3+256*(ty+2)] = (u8)252;
		
		bmpbuffer[tx+2+256*(ty-3)] = (u8)252;
		bmpbuffer[tx+1+256*(ty-3)] = (u8)252;
		bmpbuffer[tx+256*(ty-3)] = (u8)252;
		bmpbuffer[tx-1+256*(ty-3)] = (u8)252;
		bmpbuffer[tx-2+256*(ty-3)] = (u8)252;
		
		bmpbuffer[tx+2+256*(ty+3)] = (u8)252;
		bmpbuffer[tx+1+256*(ty+3)] = (u8)252;
		bmpbuffer[tx+256*(ty+3)] = (u8)252;
		bmpbuffer[tx-1+256*(ty+3)] = (u8)252;
		bmpbuffer[tx-2+256*(ty+3)] = (u8)252;
	}
	return;
}

void subscrn::update_subscreen()
{

	dmaCopy(bmpbuffer, bgGetGfxPtr(bg3), 49152);
	return;
}

void subscrn::clr_subscreen()
{
	
	bmpbuffer = 0;
	return;
}

void subscrn::pal_subscreen()
{
	int i,j;
	// Set the buffer to show the palette
	for(i=0;i<(16);i++)
	{
		for(j=0;j<(16);j++)
		{
			//dmaCopy((const void*)((u8)(i+j*16)), bgGetGfxPtr(bg3)+(i+j*16)*8, 8);
			int x = i*4, y = j*4;
			bmpbuffer[x+y*256] = i+j*16;
			bmpbuffer[x+(y+1)*256] = i+j*16;
			bmpbuffer[x+(y+2)*256] = i+j*16;
			bmpbuffer[x+(y+3)*256] = i+j*16;
			
			bmpbuffer[x+1+y*256] = i+j*16;
			bmpbuffer[x+1+(y+1)*256] = i+j*16;
			bmpbuffer[x+1+(y+2)*256] = i+j*16;
			bmpbuffer[x+1+(y+3)*256] = i+j*16;
			
			bmpbuffer[x+2+y*256] = i+j*16;
			bmpbuffer[x+2+(y+1)*256] = i+j*16;
			bmpbuffer[x+2+(y+2)*256] = i+j*16;
			bmpbuffer[x+2+(y+3)*256] = i+j*16;
			
			bmpbuffer[x+3+y*256] = i+j*16;
			bmpbuffer[x+3+(y+1)*256] = i+j*16;
			bmpbuffer[x+3+(y+2)*256] = i+j*16;
			bmpbuffer[x+3+(y+3)*256] = i+j*16;
		}
	}
	

	return;
}



